<?php
App::uses('Sanitize', 'Utility');
class CommentsController extends AppController {
	public $name = 'Comments';
	public $components = array('Session', 'Security', 'Auth');

	public function index($articleId = null){
		$this->set('articleId', $articleId);
		$this->set('comments', $this->Comment->find('all', array('conditions' => array('Comment.article_id' => $articleId))));
	}


	public function delete(){
		if($this->request->is("post")){
			$i = 0;
			foreach($this->request->data['Comment']['selectedComments'] as $commentId){
				$this->Comment->article_id = $this->request->data['Comment']['article_id'];
				$this->Comment->id = $commentId;
				if($this->Comment->delete()){
					$i++;
				}
			}
			$this->Session->setFlash($i . ": Comments have been deleted");
			$this->redirect(array("controller" => "comments", "action" => "index/" . $this->request->data['Comment']['article_id']));
		}
	}
}